package com.masterclient.app.modules.cart;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.R;
import com.masterclient.app.modules.cart.adapter.ShopCartCollectAdapter;
import com.masterclient.app.modules.cart.beans.DealerProductsBean;

import java.util.List;

/**
 * 作者：hxm
 * 创建时间：2017/9/5 10:18
 * 功能说明：
 */
public class ShopCartCollectDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private ShopCartCollectAdapter shopCartCollectAdapter;
    private List<DealerProductsBean> mData;
    private ConfirmOrderListener mConfirmOrderListener;
    private ShopCartDoDataUtil shopCartDoDataUtil;
    private String saleType;

    private TextView tvConfirm;

    ShopCartCollectDialog(@NonNull Context context, List<DealerProductsBean> data, ConfirmOrderListener confirmOrderListener) {
        super(context, R.style.MyDialog);
        mContext = context;
        mData = data;
        mConfirmOrderListener = confirmOrderListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_shop_cart_collect);
        setCanceledOnTouchOutside(true);

        shopCartDoDataUtil = new ShopCartDoDataUtil();
        ImageView ivCancel = findViewById(R.id.iv_cancel);
        TextView tvBack = findViewById(R.id.tv_back);
        tvConfirm = findViewById(R.id.tv_confirm);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);

        ivCancel.setOnClickListener(this);
        tvBack.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        shopCartCollectAdapter = new ShopCartCollectAdapter(mContext, mData.size(), (position, isCheck, saleType) -> {
            if (isCheck) {
                //选中
                mData.get(position).isChecked = true;
                if ("3".equals(mData.get(position).type)) {
                    //普通商品
                    for (int i = 0; i < mData.size(); i++) {
                        mData.get(i).isChecked = "4".equals(mData.get(i).type) || "3".equals(mData.get(i).type);
                    }
                } else {
                    //其他商品：经销商、微商
                    for (int i = 0; i < mData.size(); i++) {
                        mData.get(i).isChecked = saleType.equals(mData.get(i).saleType);
                    }
                }
                tvConfirm.setText(String.format("结算（%s）", mData.get(position).totalCartNum));
            } else {
                //取消选中
                mData.get(position).isChecked = false;
                for (int i = 0; i < mData.size(); i++) {
                    mData.get(i).isChecked =false;
                }
                tvConfirm.setText("结算");
            }
            this.saleType = saleType;
            shopCartCollectAdapter.setNewData(mData);
        });
        recyclerView.setAdapter(shopCartCollectAdapter);
        shopCartCollectAdapter.bindToRecyclerView(recyclerView);
        shopCartCollectAdapter.setNewData(mData);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowParams = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        windowParams.height = (int) (ScreenUtils.getScreenHeight() * 0.73);
        windowParams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowParams);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.iv_cancel:
            case R.id.tv_back:
                dismiss();
                break;
            case R.id.tv_confirm:
                if (shopCartDoDataUtil.isChecked(mData)) {
                    mConfirmOrderListener.onClickConfirmOrder(shopCartDoDataUtil.getDialogBuyProduct(mData), saleType);
                } else {
                    ToastUtil.showToast("请选中指定商品结算");
                }
                break;
        }
    }

    interface ConfirmOrderListener {
        void onClickConfirmOrder(String products, String saleType);
    }
}
